<template>
    <div class="registerPage">
        <el-card class="register-card">
            <div slot="header" class="clearfix">
                <span>注册</span>
            </div>
            <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px"
                     class="register-form">
            <el-form-item label="用户名" prop="name">
            <el-input v-model="ruleForm.userName"></el-input>
            </el-form-item>
            <el-form-item label="真实姓名" prop="name">
                <el-input v-model="ruleForm.userRealName"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="name">
                <el-input v-model.number="ruleForm.userPhone"></el-input>
            </el-form-item>
            <el-form-item label="性别" prop="resource">
                <el-radio-group v-model="ruleForm.userGender">
                    <el-radio label="男"></el-radio>
                    <el-radio label="女"></el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.userPwd" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
            </el-form-item>

                <el-button type="primary" class="registerBtu" @click="submitForm('ruleForm')">注册</el-button>
                <el-row>
                    <el-link class="registerToLogin" type="primary" @click="Jump">使用已有账户登录</el-link>
                </el-row>
            </el-form>
        </el-card>
    </div>
</template>

<script>
    import {register} from "../../service/user";

    export default {
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.userPwd) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    userName: '',
                    userRealName: '',
                    userPhone: '',
                    userGender: '',
                    userPwd: '',
                    checkPass: '',
                },
                rules: {
                    userName: [
                        { required: true, message: '请输入用户名', trigger: 'blur' },
                        { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
                    ],
                    userRealName: [
                        { required: true, message: '请输入真实姓名', trigger: 'blur' },
                        { min: 2, max: 8, message: '长度在 2 到 8 个字符', trigger: 'blur' }
                    ],
                    userPhone: [
                        { required: true, message: '电话号码', trigger: 'blur' },
                        { min: 11, max: 11, message: '长度在11个字符的数字', trigger: 'blur' }
                    ],
                    userPwd: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                }
            };
        },
        methods: {
            submitForm(formName) {
                //   validate一次性校验方法
                this.$refs[formName].validate(async (valid) => {
                    if (valid) {
                        let response = await register(this.ruleForm);
                        if (response.status == 1) {
                            alert("添加成功");
                            this.$router.push("/login");
                        } else {
                            alert("添加失败");
                        }
                    } else {
                        console.log("error submit!!");
                        return false;
                    }
                });
            },
            Jump() {
                //   resetFields对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
                this.$router.push("/login");
            },
        }
    }
</script>

<style scoped>
    .registerPage {
        margin-top: 20px;
    }
    .register-card {
        width: 500px;
        text-align: center;
        margin: auto;
        height: 550px;
    }
    .register-form {
        padding: 10px 50px;
    }
    .register-form .el-form-item {
        margin-bottom: 20px;
    }
    .register-form .el-input {
        width: 240px;
    }
    .register-form .el-link {
        float: right;
    }
    .registerBtu {
        margin: 10px 0;
        width: 120px;
    }
</style>